package com.adeng.springbootdemo.cms.converter;

import org.dozer.CustomConverter;
import org.dozer.MappingException;

import java.util.Date;

/**
 * Date 和 Long 转换
 *
 * @author hzwengcheng 2018-06-29 17:02
 */
public class DateAndLongCustomConverter implements CustomConverter {

    @Override
    public Object convert(Object destination, Object source,
                          Class destClass, Class sourceClass) {

        if (source == null) {
            return null;
        }

        if (source instanceof Date) {
            // check to see if the object already exists
            return ((Date) source).getTime();
        } else if (source instanceof Long) {
            return new Date((Long) source);
        } else {
            throw new MappingException("Converter DateAndLongCustomConverter "
                    + "used incorrectly. Arguments passed in were:"
                    + destination + " and " + source);
        }
    }
}
